import React from "react"
import { useNavigate } from "react-router-dom";
import { Layout, Menu, message } from 'antd';
import { UserOutlined,LoginOutlined  } from '@ant-design/icons';
import   { post }  from '../../util/post'
const { Header } = Layout;
/**
 * type=0 访客
 * type=1 用户
 */

export default function MyHeader(props) {
    const navigate = useNavigate()
    const headerMenu=[[
        {
            key:1,
            icon:<UserOutlined />,
            label:'登录',
            onClick:()=>{
                navigate('/login')
             }
        }
    ],[
        {
            key:3,
            label:'我是商家',
            onClick:()=>{
                navigate('/store')
            }
        },
        {
            key:4,
            icon:<UserOutlined />,
            children:[
                {
                    key:6,
                    label:'我的订单',
                    onClick:()=>{
                        navigate('/home/orderlist')
                    }
                },
                {
                    key:7,
                    label:"地址管理",
                    onClick:()=>{
                        navigate('/home/address')
                    }
                }
            ]
        },{
            key:5,
            icon:<LoginOutlined />,
            onClick:()=>{
                post('/auth/logout').then(res=>{
                    if(res.data.code===0)
                    {
                        message.success('已登出')
                        navigate('/login',{replace:true})
                        localStorage.removeItem('TOKEN')
                        localStorage.removeItem('roles')
                    }
                    else
                    message.error(res.data.errorMsg)
                })

            }
        }
    ]
]

    return (
        <Header>

            <Menu
                theme="dark"
                mode="horizontal"
                // defaultSelectedKeys={['2']}
                items={headerMenu[props.type]}
            />
        </Header>
    )

}
